package com.xoa.plugin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xoa.dao.vehicle.VehicleUsageMapper;
import com.xoa.dao.work.WorkMapper;
import com.xoa.dao.workflow.FlowRunPrcsMapper;
import com.xoa.global.proxy.TriggerSubject;
import com.xoa.model.nccloud.NCCInterfaceLog;
import com.xoa.model.nccloud.NccPsninfo;
import com.xoa.model.workflow.FlowRunPrcs;
import com.xoa.model.workflow.TriggerPlugIn;
import com.xoa.service.nccloud.INCCInterFaceLogService;
import com.xoa.service.nccloud.INCCPsndocService;
import com.xoa.service.nccloud.INCCloudService;
import com.xoa.util.DateUtil;
import com.xoa.util.ToJson;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  flow_plugin 流程审批回退触发器
 *  回退节点触发
 *
 */
@Service
public class WorkFlowBanckTrigger {

    //表数据
    private TriggerPlugIn tri = null;
    @Autowired
    private INCCloudService nccloudService;
    @Autowired
    INCCInterFaceLogService logService;
    //人员对象
    private  NccPsninfo nccPsninfo=null;
    @Autowired
    INCCPsndocService psndocService;
    @Autowired
    private WorkMapper workMapper;
    @Autowired
    FlowRunPrcsMapper flowRunPrcsMapper;

    public ToJson doRun(TriggerPlugIn tri) throws Exception{

        this.tri = tri;
        String run_id = tri.getRunId();
        String tableName = tri.getTabId();
        ToJson toJson=new ToJson();

        try {
//=============================城投
            /**
             * 判断是否为内部调动人员调动办理接口
             */
            if (StringUtils.isNotBlank(run_id) && tableName.equals("292") ){



                try{
                    String pk=null;
                    //=====1、单据查询
                    Map<String,Object> map=new HashedMap();
                    map.put("tableName","flow_data_292");
                    map.put("runId",run_id);
                    Map<String, Object> rsmap = workMapper.select(map);
                    // 接口调用成功 -- 取NC单据主键 pk_hi_stapply 存到记录行中
                    pk = rsmap.get("pk_hi_stapply").toString();
                    String billtype = rsmap.get("billsType").toString();


                    //=====2、提交NC单据动作
                    //{"pk":"1001ZZ100000002R2LFJ","billType":"6115","checkNote":"","approveResult":"Y","action":"APPROVE"}
                    //RECALL=收回
                    JSONObject[] jsonObjectRs1 = this.billsAction(pk, billtype, "", "", "RECALL");

                    //判断请求是否错误
                    if (jsonObjectRs1[1].getIntValue("code") == 1) {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs1[0].toJSONString(), "城投个人内部调动292run_id:"+run_id+"回退制单人流程收回动作true", jsonObjectRs1[1].toJSONString(),"1");


                        //DELETE=删除
                        JSONObject[] jsonObjectRs2 = this.billsAction(pk, billtype, "", "", "DELETE");

                        //判断请求是否错误
                        if (jsonObjectRs2[1].getIntValue("code") == 1) {
                            //请求发送保存json日志保存
                            this.saveLog(pk, jsonObjectRs2[0].toJSONString(), "城投个人内部调动292run_id:"+run_id+"回退制单人流程删除动作true", jsonObjectRs2[1].toJSONString(),"1");
                            toJson.setFlag(0);
                        } else {
                            //请求发送保存json日志保存
                            this.saveLog(pk, jsonObjectRs2[0].toJSONString(), "城投个人内部调动292run_id:"+run_id+"回退制单人流程删除动作false", jsonObjectRs2[1].toJSONString(),"0");
                            toJson.setMsg(jsonObjectRs2[1].getString("message"));
                            toJson.setFlag(1);
                        }
                    } else {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs1[0].toJSONString(), "城投个人内部调动292run_id:"+run_id+"回退制单人流程收回动作false", jsonObjectRs1[1].toJSONString(),"0");
                        toJson.setMsg(jsonObjectRs1[1].getString("message"));
                        toJson.setFlag(1);
                    }




                }catch (Exception e){
                    this.saveLog("","" ,"城投个人内部调动292run_id:"+run_id+"回退制单人流程动作false", e.getMessage(),"0");
                    toJson.setMsg(e.getMessage());
                    toJson.setFlag(1);
                }

                /**
                 * 个人离职表单接口
                 */
            }else if (tableName.equals("255")){



                try{
                    String pk=null;
                    //=====1、单据查询
                    Map<String,Object> map=new HashedMap();
                    map.put("tableName","flow_data_255");
                    map.put("runId",run_id);
                    Map<String, Object> rsmap = workMapper.select(map);
                    // 接口调用成功 -- 取NC单据主键 pk_hi_stapply 存到记录行中
                    pk = rsmap.get("pk_hi_stapply").toString();
                    String billtype = rsmap.get("billsType").toString();


                    //=====2、提交NC单据动作
                    //{"pk":"1001ZZ100000002R2LFJ","billType":"6115","checkNote":"","approveResult":"Y","action":"APPROVE"}
                    //RECALL=收回
                    JSONObject[] jsonObjectRs1 = this.billsAction(pk, billtype, "", "", "RECALL");

                    //判断请求是否错误
                    if (jsonObjectRs1[1].getIntValue("code") == 1) {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs1[0].toJSONString(), "城投个人离职255run_id:"+run_id+"回退制单人流程收回动作true", jsonObjectRs1[1].toJSONString(),"1");


                        //DELETE=删除
                        JSONObject[] jsonObjectRs2 = this.billsAction(pk, billtype, "", "", "DELETE");

                        //判断请求是否错误
                        if (jsonObjectRs2[1].getIntValue("code") == 1) {
                            //请求发送保存json日志保存
                            this.saveLog(pk, jsonObjectRs2[0].toJSONString(), "城投个人离职255run_id:"+run_id+"回退制单人流程删除动作true", jsonObjectRs2[1].toJSONString(),"1");
                            toJson.setFlag(0);
                        } else {
                            //请求发送保存json日志保存
                            this.saveLog(pk, jsonObjectRs2[0].toJSONString(), "城投个人离职255run_id:"+run_id+"回退制单人流程删除动作false", jsonObjectRs2[1].toJSONString(),"0");
                            toJson.setFlag(1);
                            toJson.setMsg(jsonObjectRs2[1].getString("message"));
                        }
                    } else {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs1[0].toJSONString(), "城投个人离职255run_id:"+run_id+"回退制单人流程收回动作false", jsonObjectRs1[1].toJSONString(),"0");
                        toJson.setFlag(1);
                        toJson.setMsg(jsonObjectRs1[1].getString("message"));
                    }



                }catch (Exception e){
                    this.saveLog("", "", "城投个人离职255run_id:"+run_id+"回退制单人流程动作false", e.getMessage(),"0");
                    toJson.setFlag(1);
                    toJson.setMsg(e.getMessage());
                }






                /**
                 * 转正
                 */
            }else if (tableName.equals("232")){



                try{
                    String pk=null;
                    //=====1、单据查询
                    Map<String,Object> map=new HashedMap();
                    map.put("tableName","flow_data_232");
                    map.put("runId",run_id);
                    Map<String, Object> rsmap = workMapper.select(map);
                    // 接口调用成功 -- 取NC单据主键 pk_hi_stapply 存到记录行中
                    pk = rsmap.get("pk_hi_stapply").toString();
                    String billtype = rsmap.get("billsType").toString();


                    //=====2、提交NC单据动作
                    //{"pk":"1001ZZ100000002R2LFJ","billType":"6115","checkNote":"","approveResult":"Y","action":"APPROVE"}
                    //RECALL=收回
                    JSONObject[] jsonObjectRs1 = this.billsAction(pk, billtype, "", "", "RECALL");

                    //判断请求是否错误
                    if (jsonObjectRs1[1].getIntValue("code") == 1) {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs1[0].toJSONString(), "城投转正232runid:"+run_id+"回退制单人流程收回动作true", jsonObjectRs1[1].toJSONString(),"1");


                        //DELETE=删除
                        JSONObject[] jsonObjectRs2 = this.billsAction(pk, billtype, "", "", "DELETE");

                        //判断请求是否错误
                        if (jsonObjectRs2[1].getIntValue("code") == 1) {
                            //请求发送保存json日志保存
                            this.saveLog(pk, jsonObjectRs2[0].toJSONString(), "城投转正232runid:"+run_id+"回退制单人流程删除动作true", jsonObjectRs2[1].toJSONString(),"1");
                            toJson.setFlag(0);
                        } else {
                            //请求发送保存json日志保存
                            this.saveLog(pk, jsonObjectRs2[0].toJSONString(), "城投转正232runid:"+run_id+"回退制单人流程删除动作false", jsonObjectRs2[1].toJSONString(),"0");
                            toJson.setFlag(1);
                            toJson.setMsg(jsonObjectRs2[1].getString("message"));
                        }
                    } else {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs1[0].toJSONString(), "城投转正232runid:"+run_id+"回退制单人流程收回动作false", jsonObjectRs1[1].toJSONString(),"0");
                        toJson.setFlag(1);
                        toJson.setMsg(jsonObjectRs1[1].getString("message"));
                    }



                }catch (Exception e){
                    this.saveLog("", "", "城投转正232runid:"+run_id+"回退制单人流程动作false", e.getMessage(),"0");
                    toJson.setFlag(1);
                    toJson.setMsg(e.getMessage());
                }


                /**
                 * 人事发起转正
                 * */
            }else if (tableName.equals("311")) {


                try {
                    String pk = null;
                    //=====1、单据查询
                    Map<String, Object> map = new HashedMap();
                    map.put("tableName", "flow_data_311");
                    map.put("runId", run_id);
                    Map<String, Object> rsmap = workMapper.select(map);
                    // 接口调用成功 -- 取NC单据主键 pk_hi_stapply 存到记录行中
                    pk = rsmap.get("pk_hi_stapply").toString();
                    String billtype = rsmap.get("billsType").toString();


                    //=====2、提交NC单据动作
                    //{"pk":"1001ZZ100000002R2LFJ","billType":"6115","checkNote":"","approveResult":"Y","action":"APPROVE"}
                    //RECALL=收回
                    JSONObject[] jsonObjectRs1 = this.billsAction(pk, billtype, "", "", "RECALL");

                    //判断请求是否错误
                    if (jsonObjectRs1[1].getIntValue("code") == 1) {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs1[0].toJSONString(), "城投人事发起转正311runid:" + run_id + "回退制单人流程收回动作true", jsonObjectRs1[1].toJSONString(), "1");


                        //DELETE=删除
                        JSONObject[] jsonObjectRs2 = this.billsAction(pk, billtype, "", "", "DELETE");

                        //判断请求是否错误
                        if (jsonObjectRs2[1].getIntValue("code") == 1) {
                            //请求发送保存json日志保存
                            this.saveLog(pk, jsonObjectRs2[0].toJSONString(), "城投人事发起转正311runid:" + run_id + "回退制单人流程删除动作true", jsonObjectRs2[1].toJSONString(), "1");
                            toJson.setFlag(0);
                        } else {
                            //请求发送保存json日志保存
                            this.saveLog(pk, jsonObjectRs2[0].toJSONString(), "城投人事发起转正311runid:" + run_id + "回退制单人流程删除动作false", jsonObjectRs2[1].toJSONString(), "0");
                            toJson.setFlag(1);
                            toJson.setMsg(jsonObjectRs2[1].getString("message"));
                        }
                    } else {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs1[0].toJSONString(), "城投人事发起转正311runid:" + run_id + "回退制单人流程收回动作false", jsonObjectRs1[1].toJSONString(), "0");
                        toJson.setFlag(1);
                        toJson.setMsg(jsonObjectRs1[1].getString("message"));
                    }


                } catch (Exception e) {
                    this.saveLog("", "", "城投人事发起转正311runid:" + run_id + "回退制单人流程动作false", e.getMessage(), "0");
                    toJson.setFlag(1);
                    toJson.setMsg(e.getMessage());
                }
                /**
                 * 人事审批离职
                 * */
            }else if (tableName.equals("303")) {


                try {
                    String pk = null;
                    //=====1、单据查询
                    Map<String, Object> map = new HashedMap();
                    map.put("tableName", "flow_data_303");
                    map.put("runId", run_id);
                    Map<String, Object> rsmap = workMapper.select(map);
                    // 接口调用成功 -- 取NC单据主键 pk_hi_stapply 存到记录行中
                    pk = rsmap.get("pk_hi_stapply").toString();
                    String billtype = rsmap.get("billsType").toString();


                    //=====2、提交NC单据动作
                    //{"pk":"1001ZZ100000002R2LFJ","billType":"6115","checkNote":"","approveResult":"Y","action":"APPROVE"}
                    //RECALL=收回
                    JSONObject[] jsonObjectRs1 = this.billsAction(pk, billtype, "", "", "RECALL");

                    //判断请求是否错误
                    if (jsonObjectRs1[1].getIntValue("code") == 1) {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs1[0].toJSONString(), "城投人事审批离职303runid:" + run_id + "回退制单人流程收回动作true", jsonObjectRs1[1].toJSONString(), "1");


                        //DELETE=删除
                        JSONObject[] jsonObjectRs2 = this.billsAction(pk, billtype, "", "", "DELETE");

                        //判断请求是否错误
                        if (jsonObjectRs2[1].getIntValue("code") == 1) {
                            //请求发送保存json日志保存
                            this.saveLog(pk, jsonObjectRs2[0].toJSONString(), "城投人事审批离职303runid:" + run_id + "回退制单人流程删除动作true", jsonObjectRs2[1].toJSONString(), "1");
                            toJson.setFlag(0);
                        } else {
                            //请求发送保存json日志保存
                            this.saveLog(pk, jsonObjectRs2[0].toJSONString(), "城投人事审批离职303runid:" + run_id + "回退制单人流程删除动作false", jsonObjectRs2[1].toJSONString(), "0");
                            toJson.setFlag(1);
                            toJson.setMsg(jsonObjectRs2[1].getString("message"));
                        }
                    } else {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs1[0].toJSONString(), "城投人事审批离职303runid:" + run_id + "回退制单人流程收回动作false", jsonObjectRs1[1].toJSONString(), "0");
                        toJson.setFlag(1);
                        toJson.setMsg(jsonObjectRs1[1].getString("message"));
                    }


                } catch (Exception e) {
                    this.saveLog("", "", "城投人事审批离职303runid:" + run_id + "回退制单人流程动作false", e.getMessage(), "0");
                    toJson.setFlag(1);
                    toJson.setMsg(e.getMessage());
                }
            }


            //================================建发
            /**
             * 个人离职表单接口
             */
            else if (tableName.equals("308")){



                try{
                    String pk=null;
                    //=====1、单据查询
                    Map<String,Object> map=new HashedMap();
                    map.put("tableName","flow_data_308");
                    map.put("runId",run_id);
                    Map<String, Object> rsmap = workMapper.select(map);
                    // 接口调用成功 -- 取NC单据主键 pk_hi_stapply 存到记录行中
                    pk = rsmap.get("pk_hi_stapply").toString();
                    String billtype = rsmap.get("billsType").toString();


                    //=====2、提交NC单据动作
                    //{"pk":"1001ZZ100000002R2LFJ","billType":"6115","checkNote":"","approveResult":"Y","action":"APPROVE"}
                    //RECALL=收回
                    JSONObject[] jsonObjectRs1 = this.billsAction(pk, billtype, "", "", "RECALL");

                    //判断请求是否错误
                    if (jsonObjectRs1[1].getIntValue("code") == 1) {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs1[0].toJSONString(), "建发个人离职308run_id:"+run_id+"回退制单人流程收回动作true", jsonObjectRs1[1].toJSONString(),"1");


                        //DELETE=删除
                        JSONObject[] jsonObjectRs2 = this.billsAction(pk, billtype, "", "", "DELETE");

                        //判断请求是否错误
                        if (jsonObjectRs2[1].getIntValue("code") == 1) {
                            //请求发送保存json日志保存
                            this.saveLog(pk, jsonObjectRs2[0].toJSONString(), "建发个人离职308run_id:"+run_id+"回退制单人流程删除动作true", jsonObjectRs2[1].toJSONString(),"1");
                            toJson.setFlag(0);
                        } else {
                            //请求发送保存json日志保存
                            this.saveLog(pk, jsonObjectRs2[0].toJSONString(), "建发个人离职308run_id:"+run_id+"回退制单人流程删除动作false", jsonObjectRs2[1].toJSONString(),"0");
                            toJson.setFlag(1);
                            toJson.setMsg(jsonObjectRs2[1].getString("message"));
                        }
                    } else {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs1[0].toJSONString(), "建发个人离职308run_id:"+run_id+"回退制单人流程收回动作false", jsonObjectRs1[1].toJSONString(),"0");
                        toJson.setFlag(1);
                        toJson.setMsg(jsonObjectRs1[1].getString("message"));
                    }



                }catch (Exception e){
                    this.saveLog("", "", "建发个人离职308run_id:"+run_id+"回退制单人流程动作false", e.getMessage(),"0");
                    toJson.setFlag(1);
                    toJson.setMsg(e.getMessage());
                }

                    /**
                     * 转正
                     */
            }else if (tableName.equals("313")) {


                try {
                    String pk = null;
                    //=====1、单据查询
                    Map<String, Object> map = new HashedMap();
                    map.put("tableName", "flow_data_313");
                    map.put("runId", run_id);
                    Map<String, Object> rsmap = workMapper.select(map);
                    // 接口调用成功 -- 取NC单据主键 pk_hi_stapply 存到记录行中
                    pk = rsmap.get("pk_hi_stapply").toString();
                    String billtype = rsmap.get("billsType").toString();


                    //=====2、提交NC单据动作
                    //{"pk":"1001ZZ100000002R2LFJ","billType":"6115","checkNote":"","approveResult":"Y","action":"APPROVE"}
                    //RECALL=收回
                    JSONObject[] jsonObjectRs1 = this.billsAction(pk, billtype, "", "", "RECALL");

                    //判断请求是否错误
                    if (jsonObjectRs1[1].getIntValue("code") == 1) {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs1[0].toJSONString(), "建发个人转正313runid:" + run_id + "回退制单人流程收回动作true", jsonObjectRs1[1].toJSONString(), "1");


                        //DELETE=删除
                        JSONObject[] jsonObjectRs2 = this.billsAction(pk, billtype, "", "", "DELETE");

                        //判断请求是否错误
                        if (jsonObjectRs2[1].getIntValue("code") == 1) {
                            //请求发送保存json日志保存
                            this.saveLog(pk, jsonObjectRs2[0].toJSONString(), "建发个人转正313runid:" + run_id + "回退制单人流程删除动作true", jsonObjectRs2[1].toJSONString(), "1");
                            toJson.setFlag(0);
                        } else {
                            //请求发送保存json日志保存
                            this.saveLog(pk, jsonObjectRs2[0].toJSONString(), "建发个人转正313runid:" + run_id + "回退制单人流程删除动作false", jsonObjectRs2[1].toJSONString(), "0");
                            toJson.setFlag(1);
                            toJson.setMsg(jsonObjectRs2[1].getString("message"));
                        }
                    } else {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs1[0].toJSONString(), "建发个人转正313runid:" + run_id + "回退制单人流程收回动作false", jsonObjectRs1[1].toJSONString(), "0");
                        toJson.setFlag(1);
                        toJson.setMsg(jsonObjectRs1[1].getString("message"));
                    }


                } catch (Exception e) {
                    this.saveLog("", "", "建发个人转正313runid:" + run_id + "回退制单人流程动作false", e.getMessage(), "0");
                    toJson.setFlag(1);
                    toJson.setMsg(e.getMessage());
                }
            }
            /**
             * 判断是否为内部调动人员调动办理接口
             */
            else if (StringUtils.isNotBlank(run_id) && tableName.equals("310") ){



                try{
                    String pk=null;
                    //=====1、单据查询
                    Map<String,Object> map=new HashedMap();
                    map.put("tableName","flow_data_310");
                    map.put("runId",run_id);
                    Map<String, Object> rsmap = workMapper.select(map);
                    // 接口调用成功 -- 取NC单据主键 pk_hi_stapply 存到记录行中
                    pk = rsmap.get("pk_hi_stapply").toString();
                    String billtype = rsmap.get("billsType").toString();


                    //=====2、提交NC单据动作
                    //{"pk":"1001ZZ100000002R2LFJ","billType":"6115","checkNote":"","approveResult":"Y","action":"APPROVE"}
                    //RECALL=收回
                    JSONObject[] jsonObjectRs1 = this.billsAction(pk, billtype, "", "", "RECALL");

                    //判断请求是否错误
                    if (jsonObjectRs1[1].getIntValue("code") == 1) {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs1[0].toJSONString(), "建发个人内部调动310run_id:"+run_id+"回退制单人流程收回动作true", jsonObjectRs1[1].toJSONString(),"1");


                        //DELETE=删除
                        JSONObject[] jsonObjectRs2 = this.billsAction(pk, billtype, "", "", "DELETE");

                        //判断请求是否错误
                        if (jsonObjectRs2[1].getIntValue("code") == 1) {
                            //请求发送保存json日志保存
                            this.saveLog(pk, jsonObjectRs2[0].toJSONString(), "建发个人内部调动310run_id:"+run_id+"回退制单人流程删除动作true", jsonObjectRs2[1].toJSONString(),"1");
                            toJson.setFlag(0);
                        } else {
                            //请求发送保存json日志保存
                            this.saveLog(pk, jsonObjectRs2[0].toJSONString(), "建发个人内部调动310run_id:"+run_id+"回退制单人流程删除动作false", jsonObjectRs2[1].toJSONString(),"0");
                            toJson.setMsg(jsonObjectRs2[1].getString("message"));
                            toJson.setFlag(1);
                        }
                    } else {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs1[0].toJSONString(), "建发个人内部调动310run_id:"+run_id+"回退制单人流程收回动作false", jsonObjectRs1[1].toJSONString(),"0");
                        toJson.setMsg(jsonObjectRs1[1].getString("message"));
                        toJson.setFlag(1);
                    }




                }catch (Exception e){
                    this.saveLog("","" ,"建发个人内部调动310run_id:"+run_id+"回退制单人流程动作false", e.getMessage(),"0");
                    toJson.setMsg(e.getMessage());
                    toJson.setFlag(1);
                }
        //=======================================省二建=================================
                /**
                 * 个人离职
                 */
            }else if (tableName.equals("314")){



                try{
                    String pk=null;
                    //=====1、单据查询
                    Map<String,Object> map=new HashedMap();
                    map.put("tableName","flow_data_314");
                    map.put("runId",run_id);
                    Map<String, Object> rsmap = workMapper.select(map);
                    // 接口调用成功 -- 取NC单据主键 pk_hi_stapply 存到记录行中
                    pk = rsmap.get("pk_hi_stapply").toString();
                    String billtype = rsmap.get("billsType").toString();


                    //=====2、提交NC单据动作
                    //{"pk":"1001ZZ100000002R2LFJ","billType":"6115","checkNote":"","approveResult":"Y","action":"APPROVE"}
                    //RECALL=收回
                    JSONObject[] jsonObjectRs1 = this.billsAction(pk, billtype, "", "", "RECALL");

                    //判断请求是否错误
                    if (jsonObjectRs1[1].getIntValue("code") == 1) {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs1[0].toJSONString(), "省二建个人离职314run_id:"+run_id+"回退制单人流程收回动作true", jsonObjectRs1[1].toJSONString(),"1");


                        //DELETE=删除
                        JSONObject[] jsonObjectRs2 = this.billsAction(pk, billtype, "", "", "DELETE");

                        //判断请求是否错误
                        if (jsonObjectRs2[1].getIntValue("code") == 1) {
                            //请求发送保存json日志保存
                            this.saveLog(pk, jsonObjectRs2[0].toJSONString(), "省二建个人离职314run_id:"+run_id+"回退制单人流程删除动作true", jsonObjectRs2[1].toJSONString(),"1");
                            toJson.setFlag(0);
                        } else {
                            //请求发送保存json日志保存
                            this.saveLog(pk, jsonObjectRs2[0].toJSONString(), "省二建个人离职314run_id:"+run_id+"回退制单人流程删除动作false", jsonObjectRs2[1].toJSONString(),"0");
                            toJson.setFlag(1);
                            toJson.setMsg(jsonObjectRs2[1].getString("message"));
                        }
                    } else {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs1[0].toJSONString(), "省二建个人离职314run_id:"+run_id+"回退制单人流程收回动作false", jsonObjectRs1[1].toJSONString(),"0");
                        toJson.setFlag(1);
                        toJson.setMsg(jsonObjectRs1[1].getString("message"));
                    }



                }catch (Exception e){
                    this.saveLog("", "", "省二建个人离职314run_id:"+run_id+"回退制单人流程动作false", e.getMessage(),"0");
                    toJson.setFlag(1);
                    toJson.setMsg(e.getMessage());
                }


                /**
                 * 转正
                 */
            }else if (tableName.equals("315")) {


                try {
                    String pk = null;
                    //=====1、单据查询
                    Map<String, Object> map = new HashedMap();
                    map.put("tableName", "flow_data_315");
                    map.put("runId", run_id);
                    Map<String, Object> rsmap = workMapper.select(map);
                    // 接口调用成功 -- 取NC单据主键 pk_hi_stapply 存到记录行中
                    pk = rsmap.get("pk_hi_stapply").toString();
                    String billtype = rsmap.get("billsType").toString();


                    //=====2、提交NC单据动作
                    //{"pk":"1001ZZ100000002R2LFJ","billType":"6115","checkNote":"","approveResult":"Y","action":"APPROVE"}
                    //RECALL=收回
                    JSONObject[] jsonObjectRs1 = this.billsAction(pk, billtype, "", "", "RECALL");

                    //判断请求是否错误
                    if (jsonObjectRs1[1].getIntValue("code") == 1) {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs1[0].toJSONString(), "省二建个人转正315runid:" + run_id + "回退制单人流程收回动作true", jsonObjectRs1[1].toJSONString(), "1");


                        //DELETE=删除
                        JSONObject[] jsonObjectRs2 = this.billsAction(pk, billtype, "", "", "DELETE");

                        //判断请求是否错误
                        if (jsonObjectRs2[1].getIntValue("code") == 1) {
                            //请求发送保存json日志保存
                            this.saveLog(pk, jsonObjectRs2[0].toJSONString(), "省二建个人转正315runid:" + run_id + "回退制单人流程删除动作true", jsonObjectRs2[1].toJSONString(), "1");
                            toJson.setFlag(0);
                        } else {
                            //请求发送保存json日志保存
                            this.saveLog(pk, jsonObjectRs2[0].toJSONString(), "省二建个人转正315runid:" + run_id + "回退制单人流程删除动作false", jsonObjectRs2[1].toJSONString(), "0");
                            toJson.setFlag(1);
                            toJson.setMsg(jsonObjectRs2[1].getString("message"));
                        }
                    } else {
                        //请求发送保存json日志保存
                        this.saveLog(pk, jsonObjectRs1[0].toJSONString(), "省二建个人转正315runid:" + run_id + "回退制单人流程收回动作false", jsonObjectRs1[1].toJSONString(), "0");
                        toJson.setFlag(1);
                        toJson.setMsg(jsonObjectRs1[1].getString("message"));
                    }


                } catch (Exception e) {
                    this.saveLog("", "", "省二建个人转正315runid:" + run_id + "回退制单人流程动作false", e.getMessage(), "0");
                    toJson.setFlag(1);
                    toJson.setMsg(e.getMessage());
                }
            }else {
                toJson.setFlag(0);
            }
        }catch (Exception e){
            e.printStackTrace();
            this.saveLog("","","回退制单人流程调用审批接口false",e.getMessage(),"0");
            toJson.setFlag(1);
            toJson.setMsg(e.getMessage());
        }

        return toJson;
    }



    /**
     * 日志保存
     * @param pk
     * @param reqeustBody
     * @param name
     * @param result
     * @throws Exception
     */
    private void saveLog(String pk,String reqeustBody,String name,String result,String state)throws Exception{
        NCCInterfaceLog ncclog = new NCCInterfaceLog();
        ncclog.setBusinessNo(pk);
        ncclog.setContent(reqeustBody);
        ncclog.setName(name);
        ncclog.setResult(result);
        ncclog.setState(state);
        ncclog.setDisposeState("0");
        logService.save(ncclog);
    }


    /**
     * 单据动作
     * @param bills
     * @return
     * @throws Exception
     */
    public JSONObject[] billsAction(String... bills)throws Exception{
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("pk",bills[0]);//单据主键
        jsonObject.put("billType",bills[1]);//单据类型编码
        jsonObject.put("checkNote",bills[2]);//批语
        jsonObject.put("approveResult",bills[3]);//审批结果
        jsonObject.put("action",bills[4]);//单据动作名称
        System.out.println("单据完成动作请求："+jsonObject.toJSONString());
        //请求发送动作
        String rs = nccloudService.sendApi(nccloudService.billactionUrl, jsonObject.toJSONString());

        return new JSONObject[]{jsonObject,JSON.parseObject(rs)};
    }
}
